<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\BookmarkService;

class BookmarkApiController extends Controller
{
    private $service;

    public function __construct(BookmarkService $service)
    {
        $this->service = $service;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $bookmarks = $this->service->all();
        return response()->json($bookmarks, 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $bookmark = $this->service->create($request->all());
        return response()->json($bookmark, 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $bookmark = $this->service->find($id);

        if (!$bookmark) {
            return response(null, 404);
        }

        return response()->json($bookmark, 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $bookmark = $this->service->update($request->all(), $id);
        return response()->json($bookmark, 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $isDeleted = $this->service->delete($id);
        if ($isDeleted) {
            return response()->json(null, 204);
        }
    }
}
